import Vec2 = cc.Vec2;
import HeroContact, {colliderGroup} from "./HeroContact";
import Music, {musicName} from "../PublicScript/Music";
import Setting from "./Setting";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Spring_box extends cc.Component {

    @property({displayName: "给出的弹力"})
    springNum: Vec2 = cc.v2(0,0);
    @property({tooltip: "动画组件", type: cc.Animation})
    animationComponent: cc.Animation = null;

    onLoad(): void {
        this.node.group = colliderGroup.springBox;
        this.node.children[1].group = colliderGroup.springBox;
        console.log(this.node.group);
    }

    onBeginContact(contact, self, other) {
        let manifold = contact.getWorldManifold();
        if(manifold.normal.y < 1){
            return;
        }
        Setting.shake(1);
        Music.GamePlayEffect(musicName.bounce);
        //播放动画
        this.animationComponent.play("spring");
        //改变英雄的状态为跳跃
        other.node.getComponent(HeroContact).jumpAnim();
        //先置空线速度
        other.body.linearVelocity = cc.v2(other.body.linearVelocity.x, 0);
        //再给其增加一个冲量
        other.body.applyLinearImpulse(this.springNum.mul(10), other.body.getWorldCenter(), true);
    }

}
